package zdream.pmfield.stage.message;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import zdream.pmfield.data.foe.BStat;
import zdream.pmfield.stage.buff.StageBuff;
import zdream.pmfield.stage.object.FoeActor;
import zdream.pmfield.stage.object.FoeActor.BStatLevelUpdateItem;
import zdream.pmfield.stage.object.FoeActor.BStatLevelUpdateReport;
import zdream.utils.StringUtils;

/**
 * @author Zdream
 * @date 2023-12-13
 * @since 0.0.1
 */
public class FoeBStatLevelChangeMessage extends StageMessage {
	public final FoeActor foe;
	public final List<LevelUpdateItem> items;
	public final String title;

	public FoeBStatLevelChangeMessage(BStatLevelUpdateReport report) {
		this.type = StageMessageType.REPORT;
		this.foe = Objects.requireNonNull(report.foe);
		this.title = Objects.requireNonNull(report.title);
		this.items = report.items.values().stream()
				.map(LevelUpdateItem::new)
				.collect(Collectors.toList());
	}

	public static class LevelUpdateItem {
		public final BStat bStat;
		public final int level;
		public final boolean change;

		public final StageBuff<?> rejectBy;
		public final boolean accept;

		public final byte levelFrom, levelTo;

		public LevelUpdateItem(BStatLevelUpdateItem item) {
			this.bStat = Objects.requireNonNull(item.bStat);
			this.level = item.level;
			this.change = item.change;
			this.rejectBy = item.rejectBy;
			this.accept = item.accept;
			if (item.rejectBy == null ^ item.accept) {
				throw new IllegalArgumentException("BStatLevelUpdateItem.reject and accept is conflicted");
			}

			this.levelFrom = item.levelFrom;
			this.levelTo = item.levelTo;
		}

		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder(100);

			builder.append(bStat).append(' ');
			if (change) {
				if (accept) {
					if (level > 0) {
						builder.append("上升了").append(' ').append(level).append(' ').append("等级");
					} else if (level < 0) {
						builder.append("下降了").append(' ').append(-level).append(' ').append("等级");
					} else {
						builder.append("不变");
					}
				} else {
					if (level > 0) {
						builder.append("原本应该上升").append(' ').append(level).append(' ').append("等级");
					} else if (level < 0) {
						builder.append("原本应该下降").append(' ').append(-level).append(' ').append("等级");
					} else {
						builder.append("原本应该不变");
					}
					builder.append('，').append("但是被").append(' ').append(rejectBy).append(' ').append("阻止了");
				}
			} else {
				builder.append("重置为等级").append(' ').append(level);
			}

			builder.append(':').append(' ').append(levelFrom).append(' ');
			builder.append("->").append(' ').append(levelTo);

			return builder.toString();
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(300);
		builder.append("[FC]").append(' ');
		foe.appendStringWithoutHp(builder);
		builder.append(' ').append("因为").append(' ').append(title).append(' ').append("改变了能力等级:");
		builder.append("\n     ");
		builder.append(StringUtils.join("\n     ", items));
		return builder.toString();
	}
}
